import cv2
import numpy as np
import PIL
import math
import matplotlib.pyplot as plt
im=cv2.imread("q1.png")
im=cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
#displaying image
plt.imshow(im)
plt.show()
# Make numpy array from image
npimage=np.array(im)
# Describe what a single red pixel looks like
red=np.array([255,0,0],dtype=np.uint8)
# Find [x,y] coordinates of all red pixels
reds=np.where(np.all((npimage==red),axis=-1))
# Describe what a single blue pixel looks like
blue=np.array([0,0,255],dtype=np.uint8)
# Find [x,y] coordinates of all blue pixels
blues=np.where(np.all((npimage==blue),axis=-1))
dx2 = (blues[0][0]-reds[0][0])**2
dy2 = (blues[1][0]-reds[1][0])**2
distance = math.sqrt(dx2 + dy2)
print("Distance between the red and blue pixel is:")
print(distance)
Distance between the red and blue pixel is: 338.3784863137726
import cv2
import matplotlib.pyplot as plt
# Read some image
img_bgr = cv2.imread('mount.jfif', 1)
img_bgr=cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_bgr)
plt.show()
# Histogram plotting of the image
color = ('b', 'g', 'r')
for i, col in enumerate(color):
histr = cv2.calcHist([img_bgr], [i], None,[256],[0, 256])
plt.plot(histr, color = col)
# Limit X - axis to 256
plt.xlim([0, 256])
plt.show()
# get height and width of the image
height, width, _ = img_bgr.shape
for i in range(0, height - 1):
for j in range(0, width - 1):
# Get the pixel value
pixel = img_bgr[i, j]
# Negate each channel by
# subtracting it from 255
# 1st index contains red pixel
pixel[0] = 255 - pixel[0]
# 2nd index contains green pixel
pixel[1] = 255 - pixel[1]
# 3rd index contains blue pixel
pixel[2] = 255 - pixel[2]
# Store new values in the pixel
img_bgr[i, j] = pixel
# Display transformed image
plt.imshow(img_bgr)
plt.show()
# Histogram plotting of the
# negative transformed image
color = ('b', 'g', 'r')
for i, col in enumerate(color):
histr = cv2.calcHist([img_bgr],
[i], None,
[256],
[0, 256])
plt.plot(histr, color = col)
plt.xlim([0, 256])
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('img.jpg',1)
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
#Applying the power law
# Apply Gamma=1.5 on the normalised image and then multiply by scaling constant (For 8 bit, c=255)
gamma = np.array(255*(img/255)**1.5,dtype='uint8')
# Display the images in subplots
img2 = cv2.hconcat([gamma])
plt.imshow(img2)
plt.show()
#gray level slicing
import cv2
import numpy as np
# Load the image
img = cv2.imread('img.jpg',1)
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img)
plt.show()
# Find width and height of image
row, column = img.shape
# Create an zeros array to store the sliced image
img1 = np.zeros((row,column),dtype = 'uint8')
# Specify the min and max range
min_range = 10
max_range = 60
# Loop over the input image and if pixel value lies in desired range set it to 255 otherwise set it to 0.
for i in range(row):
for j in range(column):
if img[i,j]>min_range and img[i,j]<max_range:
img1[i,j] = 255
else: img1[i,j] = 0
# Display the image
plt.imshow(img1)
plt.show()
#CONTRAST STRECHING
import cv2
import numpy as np
img = cv2.imread('img.jpg')
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
original = img.copy()
xp = [0, 64, 128, 192, 255]
fp = [0, 16, 128, 240, 255]
x = np.arange(256)
table = np.interp(x, xp, fp).astype('uint8')
img = cv2.LUT(img, table)
plt.imshow(original)
plt.show()
plt.imshow(img)
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('img.jpg')
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
nx=3
ny=3
imgn = np.zeros_like(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
imgN = img[max(0,i-nx):min(img.shape[0],nx+i),max(0,j-ny):min(img.shape[1],ny+j)]
imgn[i][j]=np.mean(imgN)
plt.imshow(imgn)
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('img.jpg')
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
nx=3
ny=3
imgn = np.zeros_like(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
imgN = img[max(0,i-nx):min(img.shape[0],nx+i),max(0,j-ny):min(img.shape[1],ny+j)]
imgn[i][j]=np.min(imgN)
plt.imshow(imgn)
plt.show()
#ADDITION
import cv2
import numpy as np
# path to input images are specified and
# images are loaded with imread command
image1 = cv2.imread('input1.jpg')
image2 = cv2.imread('input2.jpg')#Remember, both images should be of equal size and depth.
plt.imshow(image1)
plt.show()
plt.imshow(image2)
plt.show()
# cv2.addWeighted is applied over the
# image inputs with applied parameters
added_img = cv2.addWeighted(image1, 0.5, image2, 0.4, 0)
plt.imshow(added_img)
plt.show()
#DIVISION
import cv2
import matplotlib.pyplot as plt
# Reading image file
img1 = cv2.imread('input1.jpg')
img2 = cv2.imread('input2.jpg')
plt.imshow(img1)
plt.show()
plt.imshow(img2)
plt.show()
fimg = cv2.divide(img1,img2 , dst=None, scale=None, dtype=None)
plt.imshow(fimg)
plt.show()
#XOR
import cv2
import matplotlib.pyplot as plt
# Reading image file
img1 = cv2.imread('sq1.png')
img2 = cv2.imread('circ1.png')
plt.imshow(img1)
plt.show()
plt.imshow(img2)
plt.show()
bitwiseXor = cv2.bitwise_xor(img1, img2)
plt.imshow(bitwiseXor)
plt.show()
#NOT
import cv2
import matplotlib.pyplot as plt
# Reading image file
img = cv2.imread('circ1.png')
plt.imshow(img)
plt.show()
bitwiseNot = cv2.bitwise_not(img)
plt.imshow(bitwiseNot)
plt.show()
from scipy import ndimage
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('sq1.png')
plt.imshow(img)
plt.show()
#rotation angle in degree
rotated = ndimage.rotate(img, 45)
plt.imshow(rotated)
plt.show()
#AFFINE TRANSFORM
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image1.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50],
[200, 50],
[50, 200]])
pts2 = np.float32([[10, 100],
[200, 50],
[100, 250]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
plt.imshow(img)
plt.show()
plt.imshow(dst)
plt.show()
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('input1.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img2 = cv2.imread('input2.jpg')
img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
plt.imshow(img2)
plt.show()
img_mean=np.zeros_like(img)
img_var=np.zeros_like(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
for k in range(img.shape[2]):
l = []
l.append(img[i][j][k])
l.append(img2[i][j][k])
img_mean[i][j][k] = np.mean(l)
img_var[i][j][k] = np.var(l)
plt.imshow(img_mean)
plt.show()
plt.imshow(img_var)
plt.show()
import cv2
from matplotlib import pyplot as plt
image = cv2.imread('image2.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print("Size of image before Downsampling: ", image.shape)
plt.imshow(image)
plt.show()
image = cv2.pyrDown(image)
print("Size of image after Downsampling: ", image.shape)
plt.imshow(image)
plt.show()
Size of image before Downsampling: (480, 720, 3)
Size of image after Downsampling: (240, 360, 3)